package com.kaoker.system.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.kaoker.system.entity.ExamLevel;
import com.kaoker.system.entity.ExamPoint;
import com.kaoker.system.entity.Exercise;
import com.kaoker.system.entity.KeyNotePoint;
import com.kaoker.system.entity.Section;
import com.kaoker.system.entity.Subject;
import com.kaoker.system.service.SectionService;

/**
 * Handles requests for the application home page.
 */
@Controller
@SessionAttributes("aa") 
public class HomeController {

	private static final Logger logger = LoggerFactory
			.getLogger(HomeController.class);
	@Autowired  
	@Qualifier("sectionService") 
	private SectionService sectionService; 

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(HttpServletRequest req) {
		logger.info("Welcome home!");
		//SectionService sectionService=new SectionService();
		System.out.println(sectionService.getSectionById(Long.valueOf(1)).getName());;
		return "home";

	}
	@RequestMapping(value = "/nav", method = RequestMethod.GET)
	public String nav(HttpServletRequest req) {
		if(req.getAttribute("elList")==null){
			List<ExamLevel> elList=new ArrayList<ExamLevel>();
			String[] names=new String[]{"初级会计师","中级会计师","高级会计师","注册会计师"};
			for(int i=0;i<names.length;i++){
				ExamLevel examlevel=new ExamLevel();
				examlevel.setId(Long.valueOf(i));
				examlevel.setName(names[i]);
				elList.add(examlevel);
			}
			req.getSession().setAttribute("elList", elList);
		}		
		return "nav";
	}
	@RequestMapping(value = "/nav1", method = RequestMethod.GET)
	public String nav(HttpServletRequest req,String id) {
		
			if(id==null)
				id="0";
			List<Subject> subList=new ArrayList<Subject>();
			for(int i=0;i<6;i++){
				Subject subject=new Subject();
				subject.setId(Long.valueOf(i));
				if(id.equals("0")){				
					subject.setName("初级会计"+i);
				}else if(id.equals("1")){
					subject.setName("中级会计"+i);
				}else if(id.equals("2")){
					subject.setName("高级会计"+i);
				}else{
					subject.setName("注册会计"+i);
				}
				subList.add(subject);
			}
			req.getSession().setAttribute("subList", subList);		
			return "home";
	}
	@RequestMapping(value = "/menu", method = RequestMethod.GET)
	public String menu(HttpServletRequest req) {
		if(req.getSession().getAttribute("subList")==null){
			List<Subject> subList=new ArrayList<Subject>();
			for(int i=0;i<6;i++){
				Subject subject=new Subject();
				subject.setId(Long.valueOf(i));
				subject.setName("初级会计"+i);
				subList.add(subject);
			}
			req.getSession().setAttribute("subList", subList);	
		}
		return "menu";
	}

	// 转到考点
	@RequestMapping(value = "/examPoint", method = RequestMethod.GET)
	public String examPoint() {
		logger.info("Welcome home!");
		return "examPoint";
	}

	@RequestMapping(value = "/knowledge", method = RequestMethod.GET)
	public String knowledge() {
		logger.info("Welcome home!");
		return "knowledge";
	}

	@RequestMapping(value = "/keyNote", method = RequestMethod.GET)
	public ModelAndView keyNote(String sid) {
		String title = "会计认证  》中级会计实务——知识点";
		String url="keyNote?sid=";
		List sections = new ArrayList();
		if (sid == null) {
			for (int i = 1; i < 10; i++) {
				Section section = new Section();
				section.setName("第" + i + "章  总论");
				section.setId(Long.valueOf(i));
				Set<Section> ss = new HashSet<Section>();
				for (int j = 1; j < 5; j++) {
					Section s1 = new Section();
					s1.setName("第" + j + "节   财务报告目标");
					s1.setId(Long.valueOf(j));
					ss.add(s1);
				}
				section.setSubSections(ss);
				sections.add(section);
				
			}
			url=url+"1";
		} else if (sid.equals("1")) {
			title = "会计认证  》<a href='keyNote'>中级会计实务<a>》第一章  总论 》第一节 财务报告目标——知识点";
			Section section = new Section();
			section.setName("第一节  财务报告目标");
			Set<Section> ss = new HashSet<Section>();
			for (int j = 1; j < 5; j++) {
				Section s1 = new Section();
				s1.setName("知识点" + j + "　财物报告目标");
				s1.setId(Long.valueOf(j));
				ss.add(s1);
			}
			section.setSubSections(ss);
			sections.add(section);
			url=url+"2";
		} else if (sid.equals("2")) {
			title = "会计认证  》<a href='keyNote'>中级会计实务<a>》第一章  总论 》第一节 财务报告目标》知识点列表——知识点";
			List<KeyNotePoint> kps=new ArrayList<KeyNotePoint>();
			Section section = new Section();
			section.setName("第一节  财务报告目标");
			for(int i=0;i<5;i++){
				KeyNotePoint keyNotePoint=new KeyNotePoint();
				keyNotePoint.setName("知识点"+i+":  财物报告目标");
				keyNotePoint.setDescription("通过POST表单传回参数:式同与url的是一致的，需要将method=RequestMethod.POST,不过有中文的话一般都用post来避免转码。一般ajax的时候用$.post而不能使用jQuery插件js方式同与url的是一致的，需要将method=RequestMethod.POST,不过有中文的话一般都用post来避免转码。一般ajax的时候用$.post而不能使用jQuery插件json的$.postJSON。下面会讲到。");
                //建立相关知识点
				Set<KeyNotePoint> kp1s=new HashSet<KeyNotePoint>();
				for(int j=0;j<4;j++){
                	KeyNotePoint kp1=new KeyNotePoint();
    				kp1.setName("会计基本假设");
    				kp1s.add(kp1);
                }
				keyNotePoint.setReferKeyNotePoints(kp1s);
				//建立相关考点
				Set<ExamPoint> eps=new HashSet<ExamPoint>();
				for(int j=0;j<4;j++){
					ExamPoint ep=new ExamPoint();
    				ep.setName("会计基本假设");
    				eps.add(ep);
                }
				keyNotePoint.setReferExamPoints(eps);
				//建立相关练习
				Set<Exercise> ecs=new HashSet<Exercise>();
				for(int j=0;j<4;j++){
					Exercise ec=new Exercise();
    				ec.setStatement("练习"+j);
    				ecs.add(ec);
                }
				keyNotePoint.setReferExercises(ecs);
				kps.add(keyNotePoint);		
			}			
			section.setKeyNotePoints(kps);
			sections.add(section);
			url=url+"3";
		}
		ModelAndView mv = new ModelAndView("keyNote");
		mv.addObject("title", title);
		mv.addObject("url",url);
		mv.addObject("sections", sections);
		return mv;
	}
	@RequestMapping(value = "/test/{id}/tt/{mid}", method = RequestMethod.GET)
	public Model test(@PathVariable String id,@PathVariable String mid,Model model) {
		model.addAttribute("a", mid+"a****"+id);
		model.addAttribute("title", "test");
		return model;
		
	}
}
